module svgDecoder;
/* Sieve of Eratosthenes prime numbers */

import tango.io.Stdout;
import tango.text.xml.Document;
//import tango.text.xml.DocPrinter;
import tango.io.device.File;
import tango.text.Regex;
import tango.util.container.LinkedList;
import tango.util.Convert;
//import Tango.text.Util;

struct xenSVGDecoder
{
	float [][] paths;
	void load(char[] path)
	{
		auto file = new File (path);
		// create an array to house the entire file
		auto content = new char [file.length()];
	
		auto bytes = file.read (content);
		file.close;
		
		auto L=new LinkedList!(float[])();
		
		auto doc = new Document!(char);
		doc.parse(content);
		
		char[] s;
		char [][]ss;
		float x,y;
		auto r = new Regex(r"\s*[A-Z|a-z]\s*");//([-+]?\d+\.\d*)\,([-+]?\d+\.\d*)");
		auto r2= new Regex(r"[ \,]");
		
		
		
		foreach (pt ; doc.query.child("svg").child("g").child("path"))
		{
			
				s=pt.attributes.name(null,"d").value;
				foreach (m;r.split(s))
				{
					
					foreach (m2;tango.text.Util.split(m," "))
						if (m2.length>0)
						{
							
							ss=tango.text.Util.split(m2,",");
							x=to!(float)(ss[0]);
							y=to!(float)(ss[1]);
							L.append([x,y]);
							//Stdout.formatln(" path point = {}/{} ",x,y);
						}
				}
				L.removeHead();
				paths=L.toArray();
				
				//Stdout.formatln(" fin path");
				
			
			break;
		}
			
	}
	
	
}

struct xenLevel
{
	int width, height;
	alias float [][] Path;
	char[] name;
	float scale;
	float[2] basePos;
	
	
	
	Path [] paths;
	
	void print()
	{
		Stdout.formatln(" Niveau {} = taille {}/{} ",name,width,height);
		Stdout.formatln(" Base en {}/{} ",basePos[0],basePos[1]);
		Stdout.formatln(" Nbr d'icebergs {} ",paths.length);
		
		
		foreach(ps;paths)
			{
				foreach(pt;ps)
					Stdout.formatln(" path point = {}/{} ",pt[0],pt[1]);
				break;	
			}
			
		
	}
	void load(char[] path)
	{
		auto file = new File (path);
		// create an array to house the entire file
		auto content = new char [file.length()];
	
		auto bytes = file.read (content);
		file.close;
		
		auto L=new LinkedList!(float[])();
		auto LPaths=new LinkedList!(Path)();
		
		auto doc = new Document!(char);
		doc.parse (content);
		
		char[] s;
		char [][]ss;
		float x,y;
		auto r = new Regex(r"\s*[A-Z|a-z]\s*");//([-+]?\d+\.\d*)\,([-+]?\d+\.\d*)");
		auto r2= new Regex(r"[ \,]");
		
		
		
		foreach (svgNode ; doc.query.child("svg"))
		{
			
			
			foreach (lvlNode ; svgNode.query.child("level"))
			{
				name=lvlNode.attributes.name(null,"name").value;
				scale=to!(float)(lvlNode.attributes.name(null,"factor").value);
			
				foreach (baseNode ; lvlNode.query.child("base"))
				{
					//Stdout.formatln(" BASE ? = {}",name);
					x=to!(float)(baseNode.attributes.name(null,"x").value);
					y=to!(float)(baseNode.attributes.name(null,"y").value);
					
					basePos[]=[x,y];
					break;
				}
			}
			
			width=tango.text.convert.Integer.parse(svgNode.attributes.name(null,"width").value);
			height=tango.text.convert.Integer.parse(svgNode.attributes.name(null,"height").value);
			foreach (pt;svgNode.query.child("g").child("path"))
			{
				s=pt.attributes.name(null,"d").value;
				foreach (m;r.split(s))
				{
					
					foreach (m2;tango.text.Util.split(m," "))
						if (m2.length>0)
						{
							
							ss=tango.text.Util.split(m2,",");
							x=to!(float)(ss[0]);
							y=to!(float)(ss[1]);
							L.append([x,y]);
							//Stdout.formatln(" path point = {}/{} ",x,y);
						}
				}
				L.removeHead();
				LPaths.append(L.toArray());
				L.clear();
				//Stdout.formatln(" fin path");
				
			}
			paths=LPaths.toArray();
			LPaths.clear();
			break;
		}
			
	}
}
/*
int main()
{
	xenLevel l;
	l.load("res/level1.svg");
	l.print();
	return 0;
}
*/